import { CustomModel } from '../types';

/**
 * 掩门的真分类
 */
export const doorCategoryId = 498;

/**
 * 从给定模型当中，过滤出某一真分类模型
 * @param models
 * @param filter
 * @param maxLevel
 */
export function modelFilter(
  models: CustomModel[],
  filter = (current: CustomModel) => current.category === doorCategoryId,
  maxLevel = 4
): CustomModel[] {
  const leftModels = [...models];
  const resultModels: CustomModel[] = [];

  while (leftModels.length) {
    const current = leftModels.shift()!;
    if (filter(current)) {
      resultModels.push(current);
    } else {
      let level = maxLevel;
      let parent: CustomModel | undefined = current;
      while (level-- > 0 && parent) {
        parent = parent.getParent();
      }
      if (!parent) {
        leftModels.push(...current.getChild(), ...current.getAccessory());
      }
    }
  }

  return resultModels;
}
